package com.group3.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group3.dao.TeacherMapper;
import com.group3.domain.StudentExample;
import com.group3.domain.Teacher;
import com.group3.domain.TeacherExample;
import com.group3.pojo.receive.TeacherReceive;
import com.group3.pojo.result.KlassResult;
import com.group3.service.ATeacherService;
import com.group3.utils.Md5;
import com.group3.utils.NotNull;
import com.group3.utils.Result;
import com.group3.utils.ResultUtil;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ATeacherServiceImpl implements ATeacherService {

    @Resource
    private TeacherMapper teacherMapper;

    /**
     * @Description: 查询教师信息
     * @Param: [teacherReceive]
     * @return: com.group3.utils.Result
     * @Author: 王林
     * @Date: 2021/6/19
     */
    @Override
    @Transactional
    public Result selectTeacher(TeacherReceive teacherReceive) {

        TeacherExample teacherExample = new TeacherExample();
        TeacherExample.Criteria criteria = teacherExample.createCriteria();

        if (NotNull.judge(teacherReceive.getTeacherId())) {
            criteria.andTeacherIdEqualTo(teacherReceive.getTeacherId());
        }
        if (NotNull.judge(teacherReceive.getTeacherName())) {
            criteria.andTeacherNameLike("%" + (teacherReceive.getTeacherName() != null ? teacherReceive.getTeacherName() : "") + "%");
        }
        if (NotNull.judge(teacherReceive.getSex())) {
            criteria.andSexEqualTo(teacherReceive.getSex());
        }
        if (NotNull.judge(teacherReceive.getCardId())) {
            criteria.andCardIdEqualTo(teacherReceive.getCardId());
        }
        if (NotNull.judge(teacherReceive.getTel())) {
            criteria.andTelEqualTo(teacherReceive.getTel());
        }

        teacherExample.setOrderByClause("teacher_id");

//        1.先开启分页
        PageHelper.startPage(teacherReceive.getPageNumber(), teacherReceive.getPageSize());
//        2.执行SQL语句
        List<Teacher> teacherList = teacherMapper.selectByExample(teacherExample);
//        3.获取分页信息
        PageInfo<Teacher> teacherPageInfo = new PageInfo<>(teacherList);

//        4.返回查询信息
        return ResultUtil.success("查询成功", teacherList, teacherPageInfo.getTotal());
    }

    /**
     * @Description: 新增教师
     * @Param: [teacherReceive]
     * @return: com.group3.utils.Result
     * @Author: 王林
     * @Date: 2021/6/19
     * @Author: HY
     * @Date: 2021/6/24 改
     */
    @Override
    @Transactional
    public Result insertTeacher(TeacherReceive teacherReceive) {

        Teacher teacher = teacherReceive.getTeacher();

        String cardId = teacher.getCardId();//获取身份证号
        String cardId1 = cardId.substring((cardId.length() - 6), cardId.length());//身份证号后6位
        String cardId2 = Md5.encodeByMD5(cardId1);
        teacher.setPassword(cardId2);

        int rs = teacherMapper.insertSelective(teacher);

        return rs > 0 ? ResultUtil.success("添加成功") : ResultUtil.error("添加失败");
//        return ResultUtil.success();
    }

    /**
     * @Description: 修改教师信息
     * @Param: [teacherReceive]
     * @return: com.group3.utils.Result
     * @Author: 王林
     * @Date: 2021/6/19
     */
    @Override
    @Transactional
    public Result updateTeacherInfo(TeacherReceive teacherReceive) {
        //修改的教师信息
        Teacher teacher = teacherReceive.getTeacher();

        //修改的条件
        TeacherExample teacherExample = new TeacherExample();
        teacherExample.createCriteria().andTeacherIdEqualTo(teacherReceive.getBeforeTeacherId());

        int rs = teacherMapper.updateByExampleSelective(teacher, teacherExample);
        return rs > 0 ? ResultUtil.success("修改成功") : ResultUtil.error("修改失败");
    }

    /**
     * @Description: 更改教师状态
     * @Param: [status, teacherId]
     * @return: com.group3.utils.Result
     * @Author: 王林
     * @Date: 2021/6/19
     */
    @Override
    @Transactional
    public Result updateTeacherStatus(Integer status, String teacherId) {

        Teacher teacher = new Teacher();
        teacher.setStatus(status);

        TeacherExample teacherExample = new TeacherExample();
        TeacherExample.Criteria criteria = teacherExample.createCriteria();
        criteria.andTeacherIdEqualTo(teacherId);
        int rs = teacherMapper.updateByExampleSelective(teacher, teacherExample);

        return rs > 0 ? ResultUtil.success("修改成功") : ResultUtil.error("修改失败");
    }

    /**
     * @Description: 判断教师工号是否重复
     * @Param: [teacherId]
     * @return: com.group3.utils.Result
     * @Author: 王林
     * @Date: 2021/6/21
     */
    @Override
    @Transactional
    public Result isExistTeacherId(String teacherId) {
        TeacherExample teacherExample = new TeacherExample();

        teacherExample.createCriteria().andTeacherIdEqualTo(teacherId);

        if (teacherMapper.selectByPrimaryKey(teacherId) != null) {
            return ResultUtil.error("该用户已存在！");
        }
        return ResultUtil.success("可以使用");
    }

    /**
     * @Description: 判断教师身份证号是否存在
     * @Param: [cardId]
     * @return: com.group3.utils.Result
     * @Author: 王林
     * @Date: 2021/6/21
     */
    @Override
    @Transactional
    public Result isExistTeacherCardId(String cardId) {
        TeacherExample teacherExample = new TeacherExample();
        teacherExample.createCriteria().andCardIdEqualTo(cardId);

        if (teacherMapper.selectByExample(teacherExample).size() > 0) {
            return ResultUtil.error("该身份证号已存在！");
        }
        return ResultUtil.success("可以使用");
    }
}
